package com.ctech.wsgen.writer;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Element;

import com.ctech.wsgen.model.Item;
import com.ctech.wsgen.model.ItemCategory;
import com.ctech.wsgen.model.Site;

public class ItemCategoryListingDom4JHtmlWriter extends AbstractItemListingDom4JHtmlWriter {

	private Logger log = Logger.getLogger(ItemCategoryListingDom4JHtmlWriter.class.getName());
	
	public static final String pageName = "Product Categories"; 
	public static final String filename = "Category_Price_List_Alphabetical.htm"; 
	public static final String metaContentName = "Product Categories List";

	List<ItemCategory> itemCategories;
	Site site;
	
	public ItemCategoryListingDom4JHtmlWriter(List<ItemCategory> itemCategories, Site site) throws Exception {
		if(itemCategories != null && itemCategories.size() != 0) {
			this.itemCategories = itemCategories;
			this.site = site;
			File file = new File(site.getSiteDir().getAbsolutePath().concat(File.separator + filename));
			if(!file.exists()) file.createNewFile();
			createHtml(file, this.site.getJavaScriptBlock());		
			if(log.isInfoEnabled()) log.info("Created " + filename + " with " + itemCategories.size() + " Item Category Listings");
		} else {
			log.error("No Item Categories to make " + filename);
		}
	}

	@Override
	public void createContentFooter(Element divContent) {
	}

	@Override
	public String getContentHeaderTitle() {
		return pageName;
	}

	@Override
	public List<Item> getItems() {
		return null;
	}

	@Override
	public String getMetaContentName() {
		return metaContentName;
	}

	@Override
	public String getTitleName() {
		return pageName;
	}

	@Override
	public void createDocContainer(Element divContainer) {
		/*
		<table cellspacing="4">
			<tr>
				<td class="Navigation" nowrap style="text-align: left">
					<b><font size="3">Access Control Systems</font></b>
				</td>
			</tr>
			<tr>
				<td class="Navigation" nowrap style="text-align: left">&nbsp;</td>
			</tr>
			<tr>
				<td class=Navigation nowrap>
					<a href=./Access_Control_Telephone_Entry_Systems_Pricelist.htm>Access Control Telephone Entry Systems</a>
				</td>
			</tr>
			<tr>
				<td class=Navigation nowrap>
					<a href=./Access_Control_Video_Badging_Pricelist.htm>Access Control Video Badging</a>
				</td>
			</tr>
			...
		 */
		Element divContent = divContainer.addElement("div").addAttribute("id", "SideBar");
		Element itemTable = divContent.addElement("table").addAttribute("cellspacing", "4");
		
		String uniqueName = "";
		for (ItemCategory itemCategory : itemCategories) {
			if(!uniqueName.equalsIgnoreCase(itemCategory.getGroupName())){
				uniqueName = itemCategory.getGroupName();
				// add Category Title row
				itemTable.addElement("tr").addElement("th").addAttribute("class", "GroupName").addAttribute("nowrap", "nowrap").addText(itemCategory.getGroupName());
			}
			// add Category link
			itemTable.addElement("tr").addElement("td").addAttribute("class", "Navigation").addAttribute("nowrap", "nowrap")
				.addElement("a").addAttribute("href", "./" + itemCategory.getFileName()).addText(itemCategory.getCatTitleName());
				//+ " - (" + itemCategory.getItems().size() + ")");
		}
		
		// create content footer
		Element footerTable = divContent.addElement("table").addElement("tr").addElement("td").addAttribute("class", "ContentFooter");
		createContentFooter(footerTable);
		
	}

	@Override
	public String createMetaTagItemContent() {
		StringBuffer content = new StringBuffer();
		if(itemCategories != null) {
			for (ItemCategory itemCategory: itemCategories) {
				content.append(itemCategory.getCatTitleName() + " , ");
			}
		}
		return content.toString();
	}

	@Override
	public String getEmailAddress() {
		return this.site.getStr_email();
	}

	@Override
	public String getWebsiteURL() {
		return this.site.getStr_websiteUrl();
	}

}
